package com.wenan.data
{
	import mx.collections.ArrayList;
	import mx.collections.IList;

	public class ParameterCollection
	{
		private var _innerList:ArrayList = new ArrayList();
		
		public function ParameterCollection()
		{
		}
		
		public function getList():IList
		{
			return _innerList;
		}
		
		public function get length():int
		{
			return _innerList.length;
		}
		
		public function getAt(index:int):Parameter
		{
			return _innerList.getItemAt(index) as Parameter;
		}
		
		public function getIndexByName(parameterName:String):int
		{
			for(var i:int=0;i<_innerList.length;i++)
			{
				var parameter:Parameter = _innerList.getItemAt(i) as Parameter;
				if (parameter.name == parameterName)
				{
					return i;
				}
			}
			return -1;
		}
		
		public function findByName(parameterName:String):Parameter
		{
			var index:int = getIndexByName(parameterName);
			if (index < 0)
			{
				return null;
			}
			return _innerList.getItemAt(index) as Parameter;
		}
		
		public function getByName(parameterName:String):Parameter
		{
			var parameter:Parameter = findByName(parameterName);
			if (parameter == null) 
			{
				throw new Error("无法找到名称为 \""+parameterName+"\" 的参数.");
			}
			return parameter;
		}
		
		public function add(parameter:Parameter):void
		{
			_innerList.addItem(parameter);	
		}
		
		public function insert(index:int, parameter:Parameter):void
		{
			_innerList.addItemAt(parameter, index);	
		}
		
		public function remove(parameter:Parameter):void
		{
			_innerList.removeItem(parameter);
		}
		
		public function removeByName(parameterName:String):void
		{
			_innerList.removeItem(findByName(parameterName));
		}
		
		public function removeAt(index:int):void
		{
			_innerList.removeItemAt(index);	
		}
		
		public function clear():void
		{
			_innerList.removeAll();
		}
		
		/**
		 * 仅对保存的是parameter对象有用
		 * */
		public function getValue(parameterName:String):String
		{
			var parameter:Parameter = findByName(parameterName);
			if (parameter)
			{
				return parameter.value;
			}
			return null;
		}
		
		public function toArray():Array
		{
			return _innerList.toArray();
		}
		
		public function push(parameterExpand:ParameterExpand):void
		{
			_innerList.addItem(parameterExpand);	
		}
		
		/**
		 * 得到保存的对象
		 * */
		public function getObj(parameterName:String):Object
		{
			var parameter:Parameter = findByName(parameterName);
			if (parameter)
			{
				return parameter.obj;
			}
			return null;
		} 
		
		/**
		 * 改变obj的值
		 * */
		public function setObj(parameterName:String, newObj:Object):void
		{
			var parameter:Parameter = findByName(parameterName);
			if (parameter)
			{
				parameter.obj = newObj;
			}
		} 
	}
}